//
//  BaseQWTableCell.m
//  117goFramework
//
//  Created by Yunhai.Wu on 15/7/29.
//  Copyright (c) 2015年 117go. All rights reserved.
//

#import "BaseQWTableCell.h"

@interface BaseQWTableCell ()
@property (nonatomic, assign) BOOL initBaseQWTableCellCompletion;
@end
@implementation BaseQWTableCell

#pragma mark KVO
-(NSKeyValueObservingOptions) observerOptionsForKeypath:(NSString*) keyPath {
    return NSKeyValueObservingOptionNew;
}

- (void)registerForKVO {
	for (NSString *keyPath in [self observableKeypaths]) {
		[self addObserver:self forKeyPath:keyPath options:[self observerOptionsForKeypath:keyPath] context:NULL];
	}
}

- (void)unregisterFromKVO {
	for (NSString *keyPath in [self observableKeypaths]) {
		[self removeObserver:self forKeyPath:keyPath];
	}
}

- (NSArray *)observableKeypaths {
	return nil;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
	if (![NSThread isMainThread]) {
		[self performSelectorOnMainThread:@selector(updateUIForKeypath:) withObject:keyPath waitUntilDone:NO];
	} else {
		[self updateUIForKeypath:keyPath];
	}
}

- (void)updateUIForKeypath:(NSString *)keyPath {
}

-(void)dealloc {
    [self unregisterFromKVO];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void) loadBaseQWTableCellSubviews {
    if (!_initBaseQWTableCellCompletion) {
        [self registerForKVO];
        _initBaseQWTableCellCompletion = YES;
    }
    [self loadSubViews];
}

-(void)loadSubViews {
    if (self.contentView) {
        [self.contentView setUserInteractionEnabled:YES];
        [self setUserInteractionEnabled:YES];
    }
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadBaseQWTableCellSubviews];
    }
    return self;
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self loadBaseQWTableCellSubviews];
    }
    return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self loadBaseQWTableCellSubviews];
    }
    return self;
}
-(id)init {
    self = [super init];
    if (self) {
        [self loadBaseQWTableCellSubviews];
    }
    return self;
}
-(void)prepareForReuse {
    [super prepareForReuse];
    [self reuseTableCell];
}
-(void) reuseTableCell {
}
+(instancetype) getInstance {
    BaseQWTableCell *instance = nil;
    @try {
        NSString *nibFilePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.nib",NSStringFromClass(self)]];
        if ([[NSFileManager defaultManager] fileExistsAtPath:nibFilePath]) {
            id o = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
            if ([o isKindOfClass:self]) {
                instance = o;
            } else {
                instance = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self getTableCellIdentifier]];
            }
        } else {
            instance = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self getTableCellIdentifier]];
        }
    }
    @catch (NSException *exception) {
        instance = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self getTableCellIdentifier]];
    }
    return instance;
}



+(NSString*) getTableCellIdentifier {
    return [[NSString alloc] initWithFormat:@"%@Identifier",NSStringFromClass(self)];
}
+(CGFloat) getDefaultRowHeight:(id) rowInfo {
    return 44.0f;
}
@end